﻿using System;
using System.IO;
using System.Linq;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using HiTeachers.Entities;
using HiTeachers.Services;

namespace HiTeachers.Web
{
    public partial class Site : MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string regTelerik = Request.Params["T"];
            if (!string.IsNullOrEmpty(regTelerik))
            {
                TelerikKeyGen.TelerikKeyGenerator_Q3_2006 k = TelerikKeyGen.TelerikKeyGenerator_Q3_2006.Instance;
                string CompanyName = "MATBAO";
                k.CompanyName = CompanyName;
                k.DomainName = "hiteachers.com";//Request.Url.Authority;
                k.DomainIP = "118.139.187.1";//Request.UserHostAddress;//System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList[0].ToString();
                string LicenseFile = MapPath("~/RadControls/LicenseFile.xml");
                var f = new FileInfo(LicenseFile);
                if (f.Exists)
                {
                    f.IsReadOnly = false;
                    k.SaveLicenseFile(LicenseFile, -1);
                    Response.Write("<!-- " + k.DomainName + " : " + k.DomainIP + ":" + k.CompanyName + " -->");
                }
                else
                    Response.Write("<!-- File: LicenseFile.xml không tìm thấy-->");
            }
            //BindingMetaData();
            if (Page.IsPostBack || !Page.User.Identity.IsAuthenticated) return;

            string userName = Page.User.Identity.Name;
            Teacher teacher = TeacherServices.GetUser(userName);
            if (teacher == null)
            {
                FormsAuthentication.SignOut();
                Response.Redirect("Default.aspx");
                Response.End();
            }
            else GreetingToMember(teacher);
        }

        private void GreetingToMember(Teacher teacher)
        {
            ltrAccount.Text = teacher.Name;
            lnkLogin.HRef = "Profile.aspx";

            ltrRegister.Text = "Thoát";
            lnkRegister.HRef = "Logout.aspx";

            if (teacher.Roles.Select(it => it.Name).ToList().Contains("Admin"))
                ltrAdmin.Text = "<a class=\"admLink\" href=\"Admin/ListMember.aspx\">Vào trang admin</a>";
        }

        
    }
}
